package server.business.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.business.mapper.BloodTransferInformationMapper;
import server.business.pojo.BloodTransferInformation;
import server.business.service.BloodInformationService;
import server.business.service.BloodTransferInformationService;

import java.util.List;


@Service
public class BloodTransferInformationServiceImpl extends ServiceImpl<BloodTransferInformationMapper, BloodTransferInformation>
        implements BloodTransferInformationService {

    @Autowired
    BloodTransferInformationMapper bloodTransferInformationMapper;
    @Autowired
    BloodInformationService bloodInformationService;

    @Override
    public List<BloodTransferInformation> getAll(String id) {

        LambdaQueryWrapper<BloodTransferInformation> queryWrapper = new LambdaQueryWrapper<BloodTransferInformation>();
        queryWrapper.eq(BloodTransferInformation::getApplyId,id);
        List<BloodTransferInformation> list= bloodTransferInformationMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 删除所给编号的血液信息
     * @param string
     */
    @Override
    public void clear(String string) {
        LambdaQueryWrapper<BloodTransferInformation> queryWrapper = new LambdaQueryWrapper<BloodTransferInformation>();
        queryWrapper.eq(BloodTransferInformation::getApplyId,string);
     bloodTransferInformationMapper.delete(queryWrapper);
    }

    @Override
    public BloodTransferInformation getBloodInformation(String bloodNumber) {
        LambdaQueryWrapper<BloodTransferInformation> queryWrapper = new LambdaQueryWrapper<BloodTransferInformation>();
        queryWrapper.eq(BloodTransferInformation::getBloodNumber,bloodNumber);
        BloodTransferInformation bloodTransferInformation = bloodTransferInformationMapper.selectOne(queryWrapper);
        return bloodTransferInformation;
    }

    /**
     * 调血审核通过后对血液状态进行更改
     * @param transferId
     * @param state
     * @return
     */
    @Override
    public Boolean setBloodStateById(String transferId, String state) {
        LambdaQueryWrapper<BloodTransferInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodTransferInformation::getApplyId,transferId);
        List<BloodTransferInformation> bloodList = bloodTransferInformationMapper.selectList(queryWrapper);
        for(BloodTransferInformation blood:bloodList){
            bloodInformationService.setStateByBloodNumber(blood.getBloodNumber(),state);
        }
        return true;
    }

    @Override
    public void add(BloodTransferInformation bloodTransferInformation){
        bloodTransferInformationMapper.insert(bloodTransferInformation);
    }
}
